/**
 * @author 03010570
 * @date 2020/07/17
 * describe:    LeetCode : 50、Pow (X,N)
 */
public class LeetCode_50 {

    public static void main(String[] args) {
        LeetCode_50 code50 = new LeetCode_50();
        System.out.println(code50.myPow(2.0,-2));
    }

    public double myPow(double x, int n) {
        return n > 0 ? powDef(x, n) : 1.0 / powDef(x, n);
    }

    private double powDef(double x, int n) {
        if (n == 0) {
            return 1;
        }
        double cur = powDef(x, n / 2);
        return ((n & 1) == 1) ? cur * cur * x : cur * cur;
    }
}
